/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package DAO;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

/**
 *
 * @author Jhon
 */
public class SQLDAO {
    static private SQLDAO daoMYSQL = null;
    private SQLDAO() {}
    static public SQLDAO getSingleton() {
        if (daoMYSQL == null) {
        daoMYSQL = new SQLDAO();
        }
        return daoMYSQL;
    }
    
    
    EntityManagerFactory emf = Persistence.createEntityManagerFactory("Arquitectura_Taller2_TomcatPU2");

    public boolean create(Object entity,StringBuffer error) {
        boolean       ret = false;
        EntityManager em  = emf.createEntityManager();
        em.getTransaction().begin();
        try {
            em.persist(entity);
            em.getTransaction().commit();
            ret = true;
        } catch (Exception e) {
            error.append(e.getMessage());
//          e.printStackTrace();
            if(em.getTransaction().isActive() && em!=null)
            em.getTransaction().rollback();
            
        } finally {
            if(em.getTransaction().isActive() && em!=null){
            em.clear();
            em.close();}
            return ret;
        }
    }
}
